/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on 13/05/2011, 15:02:57
 */
package rdr.xfiles.explorer2;

import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import rdr.xfiles.model.filesystem.FileSystemModel;

/**
 *
 * @author ruanor
 */
public class DirectoryTreeViewPanel extends javax.swing.JPanel{

    private DefaultMutableTreeNode top;
    private FileSystemModel model;
    
    public DirectoryTreeViewPanel() {
        initComponents();
        setup();
    }

    void setModel(FileSystemModel model) {
        this.model = model;
    }
    
    private void setup() {
        tree.setRootVisible(false);
        tree.setShowsRootHandles(true);
        tree.putClientProperty("JTree.lineStyle", "Angled");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneTreeView = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();

        jScrollPaneTreeView.setMinimumSize(new java.awt.Dimension(200, 23));
        jScrollPaneTreeView.setName("jScrollTreeView"); // NOI18N

        tree.setName("tree"); // NOI18N
        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });
        jScrollPaneTreeView.setViewportView(tree);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneTreeView, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneTreeView, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
        //this.dirController.setCurrentDirectory(evt.getPath().toString().toString());
        System.out.println(evt.getPath().toString().toString());
    }//GEN-LAST:event_treeValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPaneTreeView;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables

    JTree getTree() {
        return this.tree;
    }

    public void setDirectories(List<String> dirs) {
        top = new DefaultMutableTreeNode("root");
        
        DefaultMutableTreeNode node = null;
        
        for(String dir:dirs)
        {
            node = new DefaultMutableTreeNode(dir);
            top.add(node);
            
            addSubNodes(node, dir);
        }
        
        TreeModel tm = new DefaultTreeModel(top);
        tree.setModel(tm);
    }

    private void addSubNodes(DefaultMutableTreeNode node, String dir) {
        //TODO: Agregar codigo para leer sub-directorios de los nodos
    }
}